package com.jctrip.hotel.common.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.jctrip.hotel.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class ExportOrderTaskThreadPool {
    private static final Integer CORE_POOL_SIZE = 4;

    private static final Integer MAXIMUM_POOL_SIZE = 9;

    private static final Long KEEP_ALIVE_TIME = 0L;

    private static final Integer QUEUE_CAPACITY_SIZE = 1024;

    private static ThreadFactory threadFactory = new ThreadFactoryBuilder()
            .setNameFormat("export-order-pool-%d").build();

    private static ExecutorService pool = new ThreadPoolExecutor(CORE_POOL_SIZE,
            MAXIMUM_POOL_SIZE,
            KEEP_ALIVE_TIME,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(QUEUE_CAPACITY_SIZE),
            threadFactory,
            new ThreadPoolExecutor.AbortPolicy());

    public static Future<?> execute(Runnable command) {
        try {
            Future<?> future = pool.submit(command);
            return future;
        } catch (Exception e) {
            log.error("export order pool execute error, command:{}, error:{}", JsonUtil.toString(command), e);
            return null;
        }
    }
}
